package com.lianzai.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {

    public static String getStringDate(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }

    public static Date transStrToDate(String str){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date trans = null;
        try {
            trans = formatter.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return trans;
    }
    /**
     * 得到指定月的天数
     * */
    public static int getMontLhastDay()
    {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH )+1;
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);//把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    public static int getMontLhasDay(int year,int month)
    {
        Calendar cal = Calendar.getInstance();
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);//把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    public static String queryNowMonth(){
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH )+1;
        if(month<10){
            return year+"-0"+month;
        }else{
            return year+"-"+month;
        }
    }
    public static int queryMonth(){
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH )+1;
        return month;
    }

    public static String queryNowYear(){
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        return year+"";
    }
    public static int queryNowYearInt(){
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        return year;
    }
    public static String queryStartFreeCarTime(){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date now = new Date();
        String nowStr = "";
        nowStr = formatter.format(now);
        return nowStr + " 00:00:00";
    }

    public static String queryCurTime(){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        String nowStr = "";
        nowStr = formatter.format(now);
        return nowStr ;
    }
    public static Date queryStartFreeCarTimeParseDate(){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        String nowStr = "";
        nowStr = formatter.format(now) + " 00:00:00";
        try {
            return formatter2.parse(nowStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String queryEndFreeCarTime(int type){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        if(type == Constants.RULE_MONTH){
            calendar.add(Calendar.MONTH,1);
            calendar.add(Calendar.DAY_OF_MONTH,-1);
        }else if(type == Constants.RULE_SEASON){
            calendar.add(Calendar.MONTH,3);
            calendar.add(Calendar.DAY_OF_MONTH,-1);
        }else if(type == Constants.RULE_YEAR){
            calendar.add(Calendar.MONTH,12);
            calendar.add(Calendar.DAY_OF_MONTH,-1);
        }
        Date endTime = calendar.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(endTime)+" 23:59:59";
    }

    public static Date queryEndFreeCarTimeParseDate(int type){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        if(type == Constants.RULE_MONTH){
            calendar.add(Calendar.MONTH,1);
            calendar.add(Calendar.DAY_OF_MONTH,-1);
        }else if(type == Constants.RULE_SEASON){
            calendar.add(Calendar.MONTH,3);
            calendar.add(Calendar.DAY_OF_MONTH,-1);
        }else if(type == Constants.RULE_YEAR){
            calendar.add(Calendar.MONTH,12);
            calendar.add(Calendar.DAY_OF_MONTH,-1);
        }
        Date endTime = calendar.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String endStr = formatter.format(endTime)+" 23:59:59";
        SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return formatter2.parse(endStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args){
        //System.out.println("start="+queryStartFreeCarTime());
        //System.out.println("end="+queryEndFreeCarTime(1));
        System.out.println("end="+getStringDate(new Date()));
    }
}
